package com.yidu.ruifeng_logistics.vo;

import com.yidu.ruifeng_logistics.controller.LogisticsInStorageController;
import com.yidu.ruifeng_logistics.entity.LogisticsInStorage;
import com.yidu.ruifeng_logistics.entity.LogisticsOrder;
import com.yidu.ruifeng_logistics.entity.LogisticsWaybill;
import com.yidu.ruifeng_logistics.service.LogisticsInStorageService;
import com.yidu.ruifeng_logistics.service.LogisticsOrderService;
import com.yidu.ruifeng_logistics.service.LogisticsWaybillService;
import com.yidu.ruifeng_logistics.utils.IdWorker;
import com.yidu.ruifeng_logistics.vo.LineVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>@Description:定时生成运单，并分配订单 </p>
 * <p>@author yidu-huang</p>
 * <p>@Date: 2021/3/12</p>
 * <p>@Time: 16:19</p>
 */
@Component
public class DingShi {

    @Resource
    LogisticsWaybillService logisticsWaybillService;
    @Resource
    LogisticsOrderService logisticsOrderService;
    @Resource
    LogisticsInStorageService logisticsInStorageService;


    public DingShi(){
        System.out.println("定时任务开启===================");
        //使用Calendar cal = Calendar.getInstance()设置时间；
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR),//cal.set(cal.get(Calendar.YEAR)当前年，月，日，时，分，秒)
                cal.get(Calendar.MONTH),
                cal.get(Calendar.DAY_OF_MONTH),
                23,55,0);
        //Date date = cal.getTime()设置开启时间为设定时间
        Date date = cal.getTime();
        //new 一个定时器 Timer timer = new Timer();
        Timer timer = new Timer();

        //定时任务
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                //需要执行的代码
//            }
//        },date,10*60*1000);


        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //需要执行的代码
                //编号生成
                IdWorker idWorker = new IdWorker();
                //根据路线分组查询订单
                List<LineVo> list=logisticsOrderService.queryLine();
                //若list.size大于0，则执行生成运单方法
                if(list.size()>0) {
                    //循环取出list
                    for (LineVo l : list) {
                        //生成唯一运单编号
                        Long waybillId = idWorker.nextId();
                        //创建运单
                        LogisticsWaybill logisticsWaybill = new LogisticsWaybill();
                        //set运单出发地目的地
                        logisticsWaybill.setWaybillBourn(l.getBourn());
                        logisticsWaybill.setWaybillDeparture(l.getDeparture());
                        //运单车辆
                        //logisticsWaybill.setCarId();
                        //set运单编号
                        logisticsWaybill.setWaybillId(waybillId);
                        //set运单状态
                        logisticsWaybill.setWaybillState("待发车");
                        //增加运单数据
                        logisticsWaybillService.insertSelective(logisticsWaybill);
                        //入库addInStorage方法
                        logisticsInStorageService.addInStorage(logisticsWaybill);
                        //new一个订单对象，set当前运单出发地、目的地和运单编号
                        LogisticsOrder logisticsOrder = new LogisticsOrder();
                        logisticsOrder.setOrderBourn(l.getBourn());
                        logisticsOrder.setOrderDeparture(l.getDeparture());
                        logisticsOrder.setWaybillId(waybillId);
                        //通过起始地修改订单表的运单号
                        logisticsOrderService.updataLine(logisticsOrder);
                    }
                }
//                logisticsWaybillService.insert()
            }
        },date,60*1000/* 定时任务间隔毫秒数*/);


    }
}
